"""
Case Type   : Compatibility
Case Name   : 表含唯一键，导入数值类型、字符类型数据，指定参数replace
Create At   : 2023/06/02
Owner       : n@ningyali
Description :
        1.建表
        2.导入数值类型数据
        3.导入字符类型数据
        4.清理环境
Expect      :
        1.创建成功
        2.成功
        3.成功
        4.成功
History     :
"""
import os
import unittest

from yat.test import Node

from testcase.COMPATIBILITY.GRAMMAR.LOAD. \
    Opengauss_Compatibility_Grammar_Load_Common import LoadData
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger


class LoadData0017(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.common = Common()
        self.load_data = LoadData()
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.pri_user = Node('PrimaryDbUser')
        self.case_lable = 'load_data_0017'
        self.db_name = f'db_{self.case_lable}'
        self.t_name_1 = f't_{self.case_lable}_1'
        self.t_name_2 = f't_{self.case_lable}_2'
        self.file_path = os.path.join('/home', self.pri_user.ssh_user)

        text1 = '--pre_step1：创建数据文件；expect:创建成功'
        self.log.info(text1)
        self.data1 = "#10,10.365456,123456.1234,10.3214,321.321,123.123654," \
                     "123.123654;#10,10.3656,12356.1234,10.24,321.321," \
                     "123.1254,123.154;"
        self.data2 = r"\$\$1,'测试','aaa','耐腐df15';" \
                     r"\$\$2,'vsfgs','ания','испытания';" \
                     r"\$\$3,'テスト',053.232,'test';"
        res = self.load_data.create_data_file(self.pri_user, self.case_lable,
                                              self.data1, self.data2)
        self.assertTrue(res, '执行失败:' + text1)

        text2 = '--pre_step2：创建兼容B库；expect:创建成功'
        self.log.info(text2)
        res = self.load_data.create_b_db(self.pri_sh, self.db_name)
        self.assertTrue(res, '执行失败:' + text2)

    def test_load(self):
        text = '----step1：建表；expect：建表成功----'
        self.log.info(text)
        cols1 = 'c_01 int,c_02 float4,c_03 float8,c_04 float(3),' \
                'c_05 binary_double,c_06 decimal(10,4),c_07 integer,' \
                'primary key(c_01,c_02,c_03)'
        cols2 = 'c_01 int,c_02 text(20),c_03 char(15),c_04 varchar(25),' \
                'primary key(c_01,c_02,c_03)'
        res1 = self.load_data.create_table(self.pri_sh, self.db_name,
                                           self.t_name_1, cols1, detail=True)
        self.assertIn('CREATE TABLE', res1, '执行失败:' + text)
        res2 = self.load_data.create_table(self.pri_sh, self.db_name,
                                           self.t_name_2, cols2, detail=True)
        self.assertIn('CREATE TABLE', res2, '执行失败:' + text)

        text = '----step2：导入数值类型数据；expect：成功----'
        self.log.info(text)
        f_path = os.path.join(self.file_path, f'{self.case_lable}_1.txt')
        res1 = self.load_data.load_data(self.pri_sh, self.db_name,
                                        self.t_name_1, f_path)
        rows, _ = self.common.parse_sql_query_res(res1)
        expect = [['10', '10.3655', '123456.1234', '10.3214', '321.321',
                   '123.1237', '123'],
                  ['10', '10.3656', '12356.1234', '10.24', '321.321',
                   '123.1254', '123']]
        self.assertEqual(expect, rows, '执行失败' + text)

        text = '----step3：导入字符类型数据；expect：成功----'
        self.log.info(text)
        f_path = os.path.join(self.file_path, f'{self.case_lable}_2.txt')
        res2 = self.load_data.load_data(self.pri_sh, self.db_name,
                                        self.t_name_2, f_path,
                                        line_start=r'\$\$')
        rows, _ = self.common.parse_sql_query_res(res2)
        expect = [['1', '测试', 'aaa', '耐腐df15'],
                  ['2', 'vsfgs', 'ания', 'испытания'],
                  ['3', 'テスト', '053.232', 'test']]
        self.assertEqual(expect, rows, '执行失败' + text)

    def tearDown(self):
        self.log.info('----清理环境----')
        res = self.load_data.clear_env(self.pri_sh, self.pri_user,
                                       self.db_name,
                                       os.path.join(self.file_path,
                                                    f"{self.case_lable}*"))
        self.assertTrue(res, '执行失败:清理环境')
        self.log.info(f'----{os.path.basename(__file__)} end----')
